﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ITOMMS.Entity;
using Watson.Core.Tools;
using ITOMMS.Business;

namespace ITOMMS.Web.Procurement
{
    public partial class ProcurementAdd : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DropDownListHelper.InitDropList<tbAssetClassEntity>(new tbAssetClassEntity().GetAll(), ddlAssetClass, "ID", "ClassName");//绑定资产类别
                DropDownListHelper.InitDropList(ddlPurchaseState, typeof(Business.PubEnums.PurchaseState));//绑定资产采购状态

                if (SessionHelper.UserRoleGroup == PubEnums.RoleGroup.客户.ToString())
                {
                    DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("ID=" + SessionHelper.UserCompanyID), ddlRequestUserID, "ID", "CompanyName");//请求人
                   
                    ddlRequestUserID.Enabled = false;
                    ddlRequestUserID.SelectedValue = SessionHelper.UserCompanyID.ToString();
                }
                else
                {
                    DropDownListHelper.InitDropList<tbUserEntity>(new tbUserEntity().GetAllByQuery("RoleGroup=" + PubEnums.RoleGroup.客户.ToString("D")), ddlRequestUserID, "ID", "CompanyName");//请求人
                }


                //lblCompanyName.Text = Business.SessionHelper.UserCompanyName;
                if (Request.QueryString["id"] != null)
                {
                    DataShow(int.Parse(Request.QueryString["id"]));
                }
                if (Request.QueryString["vid"] != null)
                {
                    ddlAssetClass.Enabled = false;
                    txtAssetDescription.Enabled = false;
                    txtAssetName.Enabled = false;
                    txtAssetNum.Enabled = false;
                    txtDeliveryTime.Enabled = false;
                    txtExpectTime.Enabled = false;
                    txtEstimatedPrice.Enabled = false;
                    ddlPurchaseState.Enabled = false;
                    this.ddlRequestUserID.Enabled = false;
                    this.txtUser.Enabled = false;
                    this.btnSave.Visible = false;
                    this.btnBack.Visible = true;

                }
            }
        }

        private void DataShow(int id)
        {
            tbPurchaseApplyEntity entity = new tbPurchaseApplyEntity();
            entity = entity.GetById(id);

            if (entity != null)
            {
                ViewState["Entity"] = entity;
                ddlAssetClass.SelectedValue = entity.AssetClassID.ToString();
                txtAssetDescription.Text = entity.AssetDescription;
                txtAssetName.Text = entity.AssetName;
                txtAssetNum.Text = entity.AssetNum.ToString();
                txtDeliveryTime.Text = entity.DeliveryTime.ToString();
                txtExpectTime.Text = entity.ExpectTime.ToString();
                txtEstimatedPrice.Text = entity.EstimatedPrice.ToString();
                ddlPurchaseState.SelectedValue = entity.PurchaseState.ToString();
                ddlRequestUserID.SelectedValue= SessionHelper.UserCompanyID.ToString();
                txtUser.Text = entity.User;
            }
        }


        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Entity.tbPurchaseApplyEntity entity = new ITOMMS.Entity.tbPurchaseApplyEntity();
                if (ViewState["Entity"] != null)
                {
                    entity = ViewState["Entity"] as tbPurchaseApplyEntity;
                }
                else
                {
                    entity.CreateBy = SessionHelper.UserID;
                    entity.CreateDate = DateTime.Now;
                }

                entity.AssetClassID = int.Parse(ddlAssetClass.SelectedValue);
                entity.AssetDescription = txtAssetDescription.Text.Trim();
                entity.AssetName = txtAssetName.Text;
                entity.AssetNum = int.Parse(txtAssetNum.Text);

                DateTime rs = new DateTime();
                if (DateTime.TryParse(txtDeliveryTime.Text.Trim(), out  rs))
                {
                    entity.DeliveryTime = rs;
                }

                DateTime et = new DateTime();
                if (DateTime.TryParse(txtExpectTime.Text.Trim(), out  et))
                {
                    entity.ExpectTime = et;
                }
                decimal ep = 0;
                if (decimal.TryParse(txtEstimatedPrice.Text.Trim(), out  ep))
                {
                    entity.EstimatedPrice = ep;
                }

                entity.PurchaseCompany = Business.SessionHelper.UserCompanyID; //采购单位。应该为登陆公司的ID
                entity.PurchaseState = int.Parse(ddlPurchaseState.SelectedValue);
                entity.User = txtUser.Text.Trim();

                entity.ModifyBy = SessionHelper.UserID;
                entity.ModifyDate = DateTime.Now;


                entity.SaveOrUpdate();
                ViewState["Entity"] = entity;
                MessageboxInfo.MessageBox(this, MessageType.SUCCEEDED, "保存成功");
            }
            catch (Exception ex)
            {
                log4netHelper.error(ex);
                MessageboxInfo.MessageBox(this, MessageType.ERROR, "保存失败");
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("ProcurementList.aspx", true);
        }
    }
}
